<template>
    <router-view></router-view>
</template>

<script>
import { Toast } from 'mint-ui'

import EventBus from 'eventbusjs'
import { mapState, mapActions } from 'vuex'

import utils from 'commons/utils'
import * as constants from 'src/AppConstants'
import * as types from 'store/mutation-types'
import storage from 'commons/storage'
import wechat from 'commons/wechat'

export default {
    components: {
    },
    data() {
        return {
        }
    },
    computed: {
        ...mapState({
            userInfo: (state) => state.userInfo,
            isTip: state => state.isTip,
            tipText: state => state.tipText,
        }),
    },

    watch: {
        isTip(value) {
            if (value) {
                if (this.toast) {
                    this.toast.message = this.tipText
                } else {
                    this.toast = Toast({message: this.tipText, duration: 2000})
                    setTimeout(() => {
                        this.$store.commit(types.UPDATE_TIP)
                        this.toast.close()
                        this.toast = null
                    }, 2000);
                }
            }
        }
    },

    methods: {
        initUser() {
            if (utils.isLogin()) {
                let apiToken = storage.getItem(constants.CACHE_API_TOKEN)
                let userInfo = storage.getItem(constants.CACHE_USER_INFO)
                this.$store.commit(types.UPDATE_API_TOKEN, apiToken)
                this.$store.commit(types.UPDATE_USER_INFO, userInfo)
                return
            }
        },
        initApp() {
            // 初始化微信SDK
            wechat.initWeChat()

            // 初始化用户信息
            this.initUser()
        }
    },

    created() {
        this.initApp()  // 初始化APP数据
    }
}
</script>
<style lang="less" rel="stylesheet/less">
@import "~assets/less/variable.less";
@import "~assets/less/common.less";
@import "~assets/fonts/iconfont.css";

html,
body {
  height: 100%;
  width: 100%;
  overflow-x: hidden;
}
</style>
